<?php

namespace tmcore\services\addons;

use think\Exception;
use tmcore\services\addons\module\Install;
use tmcore\services\addons\module\Uninstall;
use tmcore\services\addons\module\Update;
use tmcore\services\CacheService;

/**
 * 插件核心服务
 */
class CoreService extends BaseService
{
    /**
     * 插件标识
     * @var string
     */
    protected string $addon_key;

    /**
     * 初始化
     * @param string $addon
     */
    public function __construct(string $addon = '')
    {
        parent::__construct();
        $this->addon_key = $addon;
    }

    /**
     * 安装插件
     * @param array $params 安装参数
     * @param bool $is_local 是否本地
     * @return void
     * @throws Exception
     */
    public function install($params = [], $is_local = false)
    {
        (new Install($this->addon_key))->main($params, $is_local);
    }

    /**
     * 卸载插件
     * @param array $params 卸载参数
     * @return void
     * @throws Exception
     */
    public function uninstall($params = [])
    {
        (new Uninstall($this->addon_key))->main($params);
    }

    /**
     * 更新操作
     * @param $params
     * @param $is_local
     * @return void
     * @throws Exception
     */
    public function update($params = [], $is_local = false)
    {
        (new Update($this->addon_key))->main($params, $is_local);
    }

    /**
     * 清理插件缓存
     * @return void
     */
    public function clearCache()
    {
        // 清理插件缓存
        CacheService::delete("addon_{$this->addon_key}_info");
        CacheService::delete("addon_{$this->addon_key}_config");
        CacheService::delete("addon_{$this->addon_key}_event");
        CacheService::delete("addon_{$this->addon_key}_command");
    }

    /**
     * 获取插件信息
     * @return array
     */
    public function getInfo()
    {
        return UtilService::getAddonsConfig($this->addon_key);
    }

}